
<#include "/head.ftl" />
<div id="pg_margins">
<div id="col1"><#include "/sidebar.ftl" /></div>
<div id="col2">

<div class="location">主机管理</div>

<div class="massaction"><a href="javascript:addHost();">添加</a></div>

<form class="form-horizontal form" id="searchForm">
	<input name="id" type="hidden" value="${id}"/>
</form>

<div style="float:left;width:100%;">
	<table id="listTable" class="table table-striped table-bordered table-hover bd_c_1" cellpadding="0" cellspacing="0"></table>
</div>

</div>			
</div>
<#include "modal.ftl" />
<script>
    var $table = $('#listTable');
    var $param = {};

    $(function () {
        // bootstrap table初始化
        $param = {
            url: '/host/list',
            striped: false,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            smartDisplay: false,
            striped: true,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            //得到查询的参数
            queryParams: function (params) {
                var temp = $("#searchForm").serializeJsonObject();
                temp["limit"] = params.limit;
                temp["offset"] = params.offset;
                return temp;
            },
            onLoadSuccess: function() {
            	$('#listTable thead tr').attr("class", "headings");
            },
            columns: [
                {field: 'id', title: '编号', align: 'center', valign: 'middle'},
                {field: 'ip', title: 'IP', align: 'center', valign: 'middle'},
                {field: 'username', title: '账号', align: 'center', valign: 'middle'},
                {field: 'password', title: '密码', align: 'center', valign: 'middle'},
                {field: 'provider', title: '供应商', align: 'center', valign: 'middle'},
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    formatter: 'statusFormatter'
                },
                {
                    field: 'id',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: 'operateFormatter',
                    clickToSelect: false
                }
            ]
        };
        $table.bootstrapTable($param);
    });

    function statusFormatter(value, row, index) {
        var status = "";
        if (value == '0') {
            status = '<span class="green">使用中</span>';
        } else if (value == '1') {
            status = '<span class="red">已出错</span>';
        } else if (value == '2') {
            status = '<span class="red">维护中</span>';
        } else if (value == '3') {
            status = '<span class="red">已停用</span>';
        } else {
            status = '-';
        }
        return status;
    }

    function operateFormatter(value, row, index) {
        var action = [];
        action.push('<a href="http://' + row.admin + '">管理</a>');
        action.push('<a href="javascript:;" onclick="updateHost(' + row.id + ')">修改</a>');
        action.push('<a href="javascript:;" onclick="deleteHost(' + row.id + ')">删除</a>');
        return action.join('&nbsp;&nbsp;|&nbsp;&nbsp;');
    }

    function searchHost() {
        $table.bootstrapTable('destroy').bootstrapTable($param);
    }

    function addHost() {
        $("#saveHost #saveForm")[0].reset();
        $("#saveHost #saveForm #id").val("");
        $("#saveHost .modal-title").html("主机添加");
        $("#saveHost").modal("show");
        $(".modal").css("z-index", 998);
        $(".modal-backdrop").css("z-index", 997);
    }

    function updateHost(id) {
        $("#saveHost #saveForm")[0].reset();
        $("#saveHost .modal-title").html("主机修改");
        $.get("/host/info?id=" + id, function(res) {
            if (!res.data) {
                showMsgFail(res.message ? res.message : "请求失败");
                return false;
            }
            $("#saveHost #id").val(res.data.id);
            $("#saveHost #ip").val(res.data.ip);
            $("#saveHost #admin").val(res.data.admin);
            $("#saveHost #username").val(res.data.username);
            $("#saveHost #password").val(res.data.password);
            $("#saveHost #provider").val(res.data.provider);
            $("#saveHost #status").val(res.data.status);
            $("#saveHost").modal("show");
            $(".modal").css("z-index", 998);
            $(".modal-backdrop").css("z-index", 997);
        });
    }
    
    function saveHost() {
        var fields = $('#saveForm').serializeArray();
        var data = {};
        $.each(fields, function(index, field) {
            if (field.value) data[field.name] = field.value;
        })
        if (!data["ip"] || !data["username"]) {
            showMsgFail("IP、账号必填");
            return false;
        }
        $.ajax({
            url: "/host/save",
            type: "POST",
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(res) {
                if (res.code != "0200") {
                    showMsgFail(res.message ? res.message : "请求失败");
                    return false;
                }
                $("#saveHost").modal("hide");
                $("#searchForm")[0].reset();
                searchHost();
            },
            error: function(e) {
                showMsgFail("请求失败");
            }
        });
    }
    
    function deleteHost(id) {
        confirm("确认删除该主机？", function() {
            $.ajax({
                url: "/host/delete?id=" + id,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(res) {
                    if (res.code != "0200") {
                        showMsgFail(res.message ? res.message : "请求失败");
                        return false;
                    }
                	$("#searchForm")[0].reset();
                    searchHost();
                },
                error: function(e) {
                    showMsgFail("请求失败");
                }
            });
        });
    }
</script>
<#include "/foot.ftl" />
